﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SachVietDLL.BLL
{
    public class SearchEngine
    {
        private BookDatabaseEntities db = new BookDatabaseEntities();

        public List<BookDetail> Search(String Title)
        {
            try
            {
                var books = from b in db.BookDetails
                            where b.book_Title.Contains(Title)
                            select b;
                return books.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /**
         * Price = 1 -> 50
         * price = 2 -> 50 -100
         * price = 3 -> 100- 200
         * price = 4 -> >200
         * 
         * */
        public List<BookDetail> Search(String Title, String Auther, String Publisher, int Price)
        {
            int priceGround, priceHight;
            switch (Price)
            {
                case 1:
                    {
                        priceGround = 0;
                        priceHight = 50;
                        break;
                    }
                case 2:
                    {
                        priceGround = 50;
                        priceHight = 100;
                        break;
                    }
                case 3:
                    {
                        priceGround = 100;
                        priceHight = 200;
                        break;
                    }
                default:
                    {
                        priceGround = 200;
                        priceHight = Int16.MaxValue;
                        break;
                    }
            }

            var booksTitePricePublisher = from b in db.BookDetails
                        where
                            b.book_Title.Contains(Title) &&
                            priceGround < b.book_Price &&
                            b.book_Price < priceHight &&
                            b.Publisher.publisher_Name.Contains(Publisher)
                        select b;

            List<BookDetail> booksTitePricePublisherAuther = new List<BookDetail>();
            foreach (BookDetail b in booksTitePricePublisher)
            {
                foreach (Auther a in b.Authers)
                    if (a.auther_Name.Contains(Auther))
                    {
                        booksTitePricePublisherAuther.Add(b);
                        break;
                    }
            }
            return booksTitePricePublisherAuther;
        }

    }
}
